package dk.itu.mobile.vo;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementWrapper;
import javax.xml.bind.annotation.XmlRootElement;

/**
 * @author niebje
 *
 */
@XmlRootElement (name ="tasklist")
public class TaskList implements Serializable {

	private static final long serialVersionUID = 1L;
	
	@XmlElementWrapper (name ="users")
	@XmlElement(name ="user")
	public List<User> users;
	@XmlElementWrapper (name ="tasks")
	@XmlElement (name ="task")
	public List<Task> tasks;
	
	public TaskList() {
		//prepare lists for tasks and users
		users = new ArrayList<User>();
		tasks = new ArrayList<Task>();
	}
	/**
	 * Method for linking actual user objects into tasks..
	 * pretty clumsy method, but it gets the job done
	 */
	public void populateTaskUsers() {
		for (Task task : tasks) {
			List <User> taskUsers = new ArrayList<User>();
			String[] attendants = task.attendants.split(", ");
			for (String attendant : attendants) {
				for (User user : users) {
					if ( attendant.equals(user.name) ) {
						taskUsers.add(user);
					}
				}
			}
			task.setUsers(taskUsers);
		}
	}
	
	public String toString(){
		String value = this.users.toString() + this.tasks.toString();
		return value;
		
	}
	
	public Task getTask(String tId) {
		
		for (Task t : this.tasks) {
			
			if (t.id.trim().equals(tId.trim())) return t;
		}
		
		return null;
	}
	
}
